<?php

class Relationships{

	public $totalMembers;
	public $offset = '0';
	public $limitPerPage = 25;
	public $loadedRows;
	public $totalPages;
	public $currentPage;

	public function __construct( Registry $registry )
	{
		$this->registry = $registry;
	}

	/**
	 * Get the types of relationships
	 * @param $cache bool - should we cache the types?
	 * @return mixed [int|array]
	 */
	public function getTypes( $cache=false )
	{
		$sql = "SELECT ID as type_id, name as type_name, plural_name as type_plural_name, mutual as type_mutual FROM relationship_types WHERE active=1";
		if( $cache == true )
		{
			$cache = $this->registry->getObject('db')->cacheQuery( $sql );
			return $cache;
		}
		else
		{
			$types = array();
			while( $row = $this->registry->getObject('db')->getRows() )
			{
				$types[] = $row;
			}
			return $types;
		}
	}

	public function getPendingRequests($userid, $profileid)
	{
		$sql = "SELECT p.name, p.photo, p.user_id, p.profile_id, p.email, n.ID from profile p, relationships n where
		(p.user_id = n.userid AND n.profileid = p.profile_id) AND n.accepted=0 AND p.user_id IN 
		(SELECT userid from relationships where friendid=$userid and friendprofileid=$profileid) AND p.profile_id IN 
		(SELECT profileid from relationships where friendid=$userid and friendprofileid=$profileid)";

		echo $sql;

		$this->registry->getObject('db')->executeQuery( $sql );
		$cachedata = $this->registry->getObject('db')->cacheQuery( $sql );

		return $cachedata;
	}

	public function getFriends($offset, $profileid, $userid, $limit)
	{
		if ($this->totalMembers == 0)
		{
			$sql = "SELECT r.profileid from relationships r where r.accepted=1 AND  (friendid = $userid and friendprofileid = $profileid) OR (userid = $userid and profileid = $profileid)";
				
			$this->registry->getObject('db')->executeQuery( $sql );
			$nums = $this->registry->getObject('db')->numRows();
			$this->totalMembers = $nums;
				
			$this->totalPages = ceil($this->totalMembers/$this->limitPerPage);
		}

		$sql = "select p.name, p.photo, p.user_id, p.profile_id, p.email, n.ID  from profile p, relationships n
				WHERE n.accepted=1 AND
				(
					( (p.user_id = n.userid AND n.friendid=$userid ) AND (n.profileid = p.profile_id aND n.friendprofileid=$profileid)) 
					OR 
					( (p.user_id = n.friendid AND n.userid=$userid) AND (p.profile_id = n.friendprofileid AND n.profileid=$profileid)) 
				) order by p.profile_id desc LIMIT $offset , $limit
				";		

		$this->registry->getObject('db')->executeQuery( $sql );

		$data = array();

		while( ($row = $this->registry->getObject('db')->getRows()))
		{
			$data[] = $row;
		}

		$this->loadedRows = $this->registry->getObject('db')->numRows();

		$cachedata = $this->registry->getObject('db')->cacheData( $data );

		return $cachedata;
	}


	public function getOnlineFriends($profile, $limit, $offset){
		$profileID = $profile->getProfileID();
		$userID = $profile->getUserID();
		$profileType = $profile->getProfileType();

		switch ($profileType)
		{
			case 2:{
				return $this->getOnlineFacebookFriends($profile, $offset, $limit);
			}
			break;
			default:{
				return $this->getOnlineFriendsFromDB($offset, $profileID, $userID, $limit);
			}
			break;
		}
	}

	public function getOnlineFriendsFromDB($offset, $profileid, $userid, $limit)
	{
		$sql = "select p.name as olf_name, p.photo as olfriendphoto, p.user_id as oluser_id, p.profile_id as olprofile_id from profile p, relationships n
				WHERE n.accepted=1 AND
				(
					( (p.user_id = n.userid AND n.friendid=$userid ) AND (n.profileid = p.profile_id aND n.friendprofileid=$profileid)) 
					OR 
					( (p.user_id = n.friendid AND n.userid=$userid) AND (p.profile_id = n.friendprofileid AND n.profileid=$profileid)) 
				) order by p.profile_id desc LIMIT $offset , $limit
				";		

		$this->registry->getObject('db')->executeQuery( $sql );

		$data = array();

		while( ($row = $this->registry->getObject('db')->getRows()))
		{
			$profileurl = 'profile/home/'.$row['oluser_id'].'/'.$row['olprofile_id'];
			$row['profileurl'] = $profileurl;
			$data[] = $row;
		}

		$this->loadedRows = $this->registry->getObject('db')->numRows();

		$cachedata = $this->registry->getObject('db')->cacheData( $data );

		return $cachedata;
	}

	public function getOnlineFacebookFriends($profile, $offset, $limit){

		$fb_connector = $profile->getInitFBConnector();

		$friendlist = $fb_connector->getFriendList($fb_connector->getUserID(), $limit, $this->offset);

		$data = array();

		foreach ($friendlist as $friend)
		{
			$row['olf_name'] = $friend['name'];
			$row['olfriendphoto'] = $friend['pic_small_with_logo'];
			$row['oluser_id'] = $friend['uid'];
			$row['olprofile_id'] = '0';
			$row['profileurl'] = $friend['profile_url'];

			$data[] = $row;
			
			++$this->offset;
		}

		$this->loadedRows = count($friendlist);

		$cachedata = $this->registry->getObject('db')->cacheData( $data );

		return $cachedata;
	}

	public function search($name, $profileid, $userid, $fullSearch)
	{
		$sql = "select p.name, p.photo, p.user_id, p.profile_id, p.email, n.ID  from profile p, relationships n
				WHERE n.accepted=1 AND p.name like '%$name%' AND
				(
					( (p.user_id = n.userid AND n.friendid=$userid ) AND (n.profileid = p.profile_id aND n.friendprofileid=$profileid)) 
					OR 
					( (p.user_id = n.friendid AND n.userid=$userid) AND (p.profile_id = n.friendprofileid AND n.profileid=$profileid)) 
				) order by p.profile_id desc LIMIT 5
				";				

		if ($fullSearch)
		$sql = "select p.name, p.photo, p.user_id, p.profile_id, p.email, n.ID  from profile p, relationships n
				WHERE n.accepted=1 AND p.name like '%$name%'  AND
				(
					( (p.user_id = n.userid AND n.friendid=$userid ) AND (n.profileid = p.profile_id aND n.friendprofileid=$profileid)) 
					OR 
					( (p.user_id = n.friendid AND n.userid=$userid) AND (p.profile_id = n.friendprofileid AND n.profileid=$profileid)) 
				) order by p.profile_id desc
				";				

		$this->registry->getObject('db')->executeQuery( $sql );

		$data = array();

		while( ($row = $this->registry->getObject('db')->getRows()))
		{
			$re_fname='<b>'.$name.'</b>';
			$row['final_fname'] = str_ireplace($name, $re_fname, $row['name']);
			$data[] = $row;
		}
		$this->loadedRows = $this->registry->getObject('db')->numRows();
		$cachedata = $this->registry->getObject('db')->cacheData( $data );

		return $cachedata;
	}
}

?>
